package com.moonshineproject.commands
{
	import com.moonshineproject.GlobalEventDispatcher;
	import com.moonshineproject.IDEModel;
	import com.moonshineproject.editor.BasicTextEditor;
	import com.moonshineproject.events.CloseTabEvent;
	import com.moonshineproject.events.DeleteFileEvent;
	import com.moonshineproject.view.IContentWindow;
	
	import flash.events.Event;

	public class DeleteFileCommand implements ICommand
	{
		public function execute(event:Event):void
		{
			var e:DeleteFileEvent = DeleteFileEvent(event);

			if (!e.file.exists) return;
			

			if (e.file.isDirectory)
			{
				e.file.deleteDirectory(true);
			}
			else
			{
				e.file.deleteFile();
			}
			
			if (e.wrapper.sourceController)
			{
				e.wrapper.sourceController.remove(e.file);
			}
			
			for each (var tab:IContentWindow in IDEModel.getInstance().editors)
			{
				var ed:BasicTextEditor = tab as BasicTextEditor;
				if (ed 
					&& ed.currentFile
					&& ed.currentFile.nativePath == e.file.nativePath)
				{
					GlobalEventDispatcher.getInstance().dispatchEvent(
						new CloseTabEvent(CloseTabEvent.EVENT_CLOSE_TAB, ed, true)
					);
				}
			}
			
		}

	}
}